<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Aboot\Attribute;
use App\Model\Admin\ProductAttr;
use App\Model\Admin\ProductType;
use Illuminate\Http\Request;

class AttributeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // return view('admin.product.attributes.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($typeId)
    {
        $types = ProductType::all();

        return view('admin.product.attributes.create', compact('types'))->with('id', $typeId);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $attr = Attribute::create([
            'attr_type'=>$request->attr_type,
            'attr_name'=>$request->attr_name,
            'attr_option_values'=>$request->attr_option_values,
            'type_id'=>$request->type_id
            ]);
        if ($attr) {
            $request->session()->flash('success', '添加成功');
            return redirect()->route('attributes.typeshow', $request->type_id)->with('id', $request->type_id);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // dd($id);
        $types = ProductType::all();
        $attrs = Attribute::where('type_id', $id)->get();
        if (request()->ajax()) {
            return response()->json($attrs, 200);
        }
        return view('admin.product.attributes.index', compact('types', 'attrs'))->with('id', $id);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id, $type_id)
    {
        $attr = Attribute::find($id);
        $types = ProductType::all();
        return view('admin.product.attributes.edit', compact('types', 'attr'))->with('id', $type_id);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $attr = Attribute::find($id);
        $attr->update([
                'attr_type'=>$request->attr_type,
                'attr_name'=>$request->attr_name,
                'attr_option_values'=>$request->attr_option_values,
                'type_id'=>$request->type_id
            ]);
        if ($attr) {
            $request->session()->flash('success', '修改成功');
            return redirect()->route('attributes.typeshow', $request->type_id)->with('id', $request->type_id);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id, $type_id)
    {
        $attr = Attribute::where('id', $id)->delete();
        // $attr->delete();
        if ($attr) {
            request()->session()->flash('success', '删除成功');
            return redirect()->route('attributes.typeshow', $type_id)->with('id', $type_id);
        }
    }

    public function del_attr($id)
    {
        ProductAttr::find($id)->delete();
    }
}
